package com.impress;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class TitleBar extends LinearLayout {

	private ImageView mSettingButton;
	private ImageView mRefreshButton;
	private ImageView mBackButton;
	private Context mContext;
	private ProgressBar mProgressBar;
	
	public TitleBar(Context context) {
		super(context);	
		mContext = context;
		initialView();
	}

	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);	
		mContext = context;
		initialView();
	}
	
	private void initialView(){
		View layout = LayoutInflater.from(mContext).inflate(R.layout.titlebar, null);
		addView(layout);
		mSettingButton = (ImageView) layout.findViewById(R.id.setting_button);
		mSettingButton.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mContext.startActivity(new Intent(mContext, PreferencesActivity.class));
				return false;
			}
		});
		mRefreshButton = (ImageView) layout.findViewById(R.id.refresh_button);
		mBackButton = (ImageView) layout.findViewById(R.id.back_button);
		mProgressBar = (ProgressBar) layout.findViewById(R.id.progressBar);
	}
	
	public ImageView getSettingButton() {
		return mSettingButton;
	}

	public ImageView getRefreshButton() {
		return mRefreshButton;
	}

	public ImageView getBackButton() {
		return mBackButton;
	}
	
	public ProgressBar getProgressBar(){
		return mProgressBar;
	}

	/**
	 * set view state, change components' visibility.
	 * @param viewState: VIEW_STATE_FEED, VIEW_STATE_ITEM
	 */
	public void setViewState(String viewState){
		if (viewState == Utility.VIEW_STATE_FEED) {
			mRefreshButton.setVisibility(View.VISIBLE);
			mBackButton.setVisibility(View.GONE);
			mSettingButton.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.GONE);
		}else {
			mBackButton.setVisibility(View.VISIBLE);
			mSettingButton.setVisibility(View.GONE);
			mRefreshButton.setVisibility(View.GONE);
		}
	}
	
	public void clearView(){
		mRefreshButton.setVisibility(View.GONE);
		mSettingButton.setVisibility(View.GONE);
	}
	
	public void setRefreshListener(OnTouchListener l){
		mRefreshButton.setOnTouchListener(l);
	}
	
	public void setBackListener(OnTouchListener l){
		mBackButton.setOnTouchListener(l);
	}
	
	public void setProgressBarVisible(boolean showProgressBar){
		if (showProgressBar) {
			mProgressBar.setVisibility(View.VISIBLE);
			mRefreshButton.setVisibility(View.GONE);
		}else {
			mProgressBar.setVisibility(View.GONE);
			mRefreshButton.setVisibility(View.VISIBLE);
		}
	}
	
	public int getRefreshButtonVisibility(){
		return mRefreshButton.getVisibility();
	}

}
